package com.lst.servlet;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.InformListReqPara;
import com.lst.model.MstClient;
import com.lst.model.Plan;
import com.lst.service.MstClientService;
import com.lst.service.PlanService;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: InformEditServlet
 * @Description: 通知管理编辑接口
 * @author zmm
 * @date 2017年10月16日 
 * 
 */
public class InformEditServlet extends BaseServlet {

    private static final long serialVersionUID = 1L;

    private MstClientService mstClientService = ctx.getBean(MstClientService.class);
    
	private PlanService planService = ctx.getBean(PlanService.class);

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
	doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
	BaseResponse resPara = new BaseResponse();

	try {
	    InformListReqPara reqPara = getReqPara(request);

	    if (isCheckField(reqPara, resPara)) {
		Integer userid = Integer.valueOf(reqPara.getUserid());
		Boolean discussStatus=reqPara.getDiscussStatus().equals("true")?true:false;
		Boolean thumbStatus=reqPara.getThumbStatus().equals("true")?true:false;
		Boolean systemStatus=reqPara.getSystemStatus().equals("true")?true:false;
		Boolean noticeStatus=reqPara.getNoticeStatus().equals("true")?true:false;
		String noticeTime=reqPara.getNoticeTime();
	
		MstClient mstClient=mstClientService.selectByPrimaryKey(Integer.valueOf(userid));
        if(mstClient!=null){
        	mstClient.setDiscussStatus(discussStatus);
        	mstClient.setThumbStatus(thumbStatus);
        	mstClient.setSystemStatus(systemStatus);
        	mstClientService.update(mstClient);
        } 
		
		Plan plan=planService.selectByUser(userid);
		if(plan!=null){
			plan.setIsNotice(noticeStatus);
			plan.setNoticeTime(noticeTime);
			planService.updatePlan(plan);
		}

		resPara.setCode(CommCode.M_Y000000);
		resPara.setMessage(CommCode.M_Y000001);
	    }
	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0001);

	    logger.error("doPost error: ", e);
	}

	try {
	    response.setContentType("application/json;charset=utf-8");
	    response.setHeader("pragma", "no-cache");
	    response.setHeader("cache-control", "no-cache");

	    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
		    .excludeFieldsWithoutExposeAnnotation().serializeNulls()
		    .create();
	    String jsonResult = gson.toJson(resPara);
//	    logger.info("InformEditServlet jsonResult: " + jsonResult);

	    PrintWriter out = response.getWriter();
	    out.print(jsonResult);
	    out.flush();
	    out.close();

//	    logger.info("doPost end run(s): "+ DateUtil.calLastedTime(startDate));
	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0003);

	    logger.error("doPost error: ", e);
	}
    }

    @Override
    public InformListReqPara getReqPara(HttpServletRequest request) {

    	InformListReqPara reqPara = null;

	try {
	    BaseRequest baseRequest = RequestUtils.getRequestPara(request,
		    new InformListReqPara());

	    reqPara = (InformListReqPara) baseRequest.clone();
	    String userid = request.getParameter("userid");
	    String discussStatus=request.getParameter("discussStatus");
	    String thumbStatus=request.getParameter("thumbStatus");
	    String systemStatus=request.getParameter("systemStatus");
	    String noticeStatus=request.getParameter("noticeStatus");
	    String noticeTime=request.getParameter("noticeTime");

//	    logger.info("reqPara userid: " + userid);
//	    logger.info("reqPara discussStatus: " + discussStatus);
//	    logger.info("reqPara thumbStatus: " + thumbStatus);
//	    logger.info("reqPara systemStatus: " + systemStatus);
//	    logger.info("reqPara noticeStatus: " + noticeStatus);
//	    logger.info("reqPara noticeTime: " + noticeTime);

	    reqPara.setUserid(userid);
	    reqPara.setDiscussStatus(discussStatus);
	    reqPara.setThumbStatus(thumbStatus);
	    reqPara.setSystemStatus(systemStatus);
	    reqPara.setNoticeStatus(noticeStatus);
	    reqPara.setNoticeTime(noticeTime);

	} catch (Exception e) {
	    logger.error("getReqPara error: ", e);
	}
	return reqPara;
    }

    @Override
    public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
	boolean flag = false;

	InformListReqPara cmReqPara = (InformListReqPara) reqPara;
	BaseResponse cmResPara = (BaseResponse) resPara;

	if (StringUtils.isEmpty(cmReqPara.getUserid())) {
	    cmResPara.setCode(CommCode.M_A000000);
	    cmResPara.setMessage(CommCode.M_P000002);
	} else {
	    flag = true;
	}
	return flag;
    }
}
